package com.blb.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Brand;
import com.blb.entity.BrandType;
import com.blb.entity.Comtype;
import com.blb.entity.Menu;
import com.blb.service.IBrandService;
import com.blb.service.IBrandTypeService;
import com.blb.service.MenuService;
import com.blb.utils.LayuiPage;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @param <T>
 * @author txy
 * @since 2019-07-10
 */
@Controller
@RequestMapping("/brand")
public class BrandController<T> {

    @Autowired
    private IBrandService brandService;

    @Autowired
    private MenuService MenuService;

    @Autowired
    private IBrandTypeService brandTypeService;

    /**
     * 跳轉
     *
     * @param model
     * @param menuid
     * @return
     */
    @RequestMapping("index")
    public String index(Model model, @Param("menuid") String menuid) {
        List<Menu> list = MenuService.getBts(menuid);
        model.addAttribute("bts", list);
        return "admin/brand/brand_list";
    }

    /**
     * 分页
     *
     * @param page
     * @param limit
     * @param markname
     * @return
     */
    @ResponseBody
    @RequestMapping("page")
    public LayuiPage page(int page, int limit, String markname) {

        //mybatisplus分页
        Page<Brand> page2 = new Page<Brand>(page, limit);
        IPage<Brand> iPage = null;
        iPage = brandService.page(page2, null);

        if (markname != null && !markname.equals("")) {
            iPage = brandService.page(page2, new QueryWrapper<Brand>().like("markname", markname));
        }

        LayuiPage layuiPage = new LayuiPage();

        layuiPage.setCount((int) iPage.getTotal());
        layuiPage.setData(iPage.getRecords());

        return layuiPage;
    }

    /**
     * 跳转添加
     *
     * @return
     */
    @RequestMapping("showAdd")
    public String showAdd(Model model) {
        //获取所有商品分类
        List<Comtype> productTypeNameList = brandService.productTypeNameList();
        model.addAttribute("comtype", productTypeNameList);
        return "admin/brand/brand_add";
    }


    /**
     * 添加商品品牌
     * 添加商品品牌与商品商品类别
     *
     * @param brand
     * @param comtypeName
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("add")
    public String add(Brand brand, @Param("comtypeName") String comtypeName) {

        try {
            brandService.add(brand, comtypeName);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加失败" + e.getMessage();
        }
    }

    /**
     * 判断品牌名称是否存在
     *
     * @param markname
     * @return
     */
    @ResponseBody
    @RequestMapping("isExist")
    public String isExist(String markname, String id) {

        if (id != null) {
            Brand brand = brandService.getById(id);
            if (brand.getMarkname().equals(markname)) {
                return "";
            }
        }
        Brand brand = brandService.getOne(new QueryWrapper<Brand>().eq("markname", markname));
        if (brand != null) {
            return "品牌名称已存在";
        }
        return "";
    }


    /**
     * 跳转修改
     *
     * @param model
     * @param brand
     * @return
     */
    @RequestMapping("showUpdate")
    public String showUpdate(Model model, Brand brand) {
        //获取所有商品分类
        List<Comtype> productTypeNameList = brandService.productTypeNameList();
        model.addAttribute("comtype", productTypeNameList);

        //获取当前品牌所属的商品分类
        List<BrandType> list = brandTypeService.getBrandTypeByBrand(brand.getId());
        String brandTypeList = "";
        if (!list.isEmpty()) {
            brandTypeList = JSON.toJSON(list).toString();
        }

        //将当前商品品牌信息返回
        model.addAttribute("brand", brand);

        //将品牌具有的类别返回
        model.addAttribute("brandTypeList", brandTypeList);

        return "admin/brand/brand_update";
    }

    /**
     * 修改商品品牌
     *
     * @param brand
     * @param comtypeName
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public String update(Brand brand, @Param("comtypeName") String comtypeName) {
        try {
            brandService.update(brand, comtypeName);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "修改失败" + e.getMessage();
        }
    }

    /**
     * 删除商品品牌
     *
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public String delete(@RequestBody List<String> ids) {

        //在service层删除品牌  并删除第三张关联表中数据
        try {
            brandService.delete(ids);
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败" + e.getMessage();
        }
    }
}

